#ifndef COMMON_H_
#define COMMON_H_

/** 系统的函数库 **/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <math.h>
/** 配置文件 **/
#include "config.h"

/** 返回参数 **/
#define KT_OK		 				 0
#define KT_ERROR					-1
#define KT_WARN						-2

/** 无符号字节类型表示 **/
#define uint8           unsigned char
#define uint16          unsigned short
#define uint32          unsigned int
#define uint64          unsigned long

/** KB大小 **/
#define SIZE_B          1
#define SIZE_KB         (SIZE_B  * 1024L)
#define SIZE_MB         (SIZE_KB * 1024L)
#define SIZE_GB         (SIZE_MB * 1024L)     

/** 工具宏定义 */
//求num/dsr且保留精度为0.1
#define FLOAT_ACCURACY(num, dsr)	  (ceil((num*10)/(dsr*1.0))/10.0)   

/** 测日志模块 **/
#include    "logger.h"
#include	"stru_queue.h"
#include 	"util-string.h"

typedef struct {
	unsigned short head;						//固定值0xC8C9
	unsigned char type;							//0x05:录取指令报文

	unsigned char luqu_status;					//0x00:停止 0x01:录取 0x02:格式化
	//光纤
	unsigned char SourceData_hecha;				//b0:光纤1:1为选中 0为未选中，光纤234类似
	unsigned char SourceData_DBF;
	unsigned char SourceData_kangganrao;
	unsigned char SourceData_ganraozhencha_g;
	unsigned short SourceData_xinhaomoni;		//此位占用两个字节
	//千兆网
	unsigned char SourceData_gengzongmubiao;	//跟踪目标二次信息
	unsigned char SourceData_kongzhixinxi;		//控制信息和状态信息
	unsigned char SourceData_waibuyindao;		//外部引导数据
	unsigned char SourceData_mubiaoshibie;		//目标识别特征库数据
	unsigned char SourceData_ganraozhencha_w;	//干扰侦查数据(千兆)
	unsigned char FileSize;						//文件大小 0x00:不限制 0x01:1GB 0x02:2GB 0x03:4GB
	unsigned int  Tasknum;						//任务代号，待定
	unsigned short year;
	unsigned char month;
	unsigned char date;
	unsigned char hour;
	unsigned char min;
	unsigned char sec;

	unsigned short tail;						//报文总长度 22byte
}__attribute__((packed)) RecvCmd;

typedef struct {
	uint16 head;
	uint8 type;
	uint32 psize;
	uint32 cycle;
	uint8 dtype; 						//0x00 光纤模拟  0x01 正常模式
	uint8 opermem;						//0x00 写内存   0x01 读内存
	uint8 status; 						//0x00 停止测试	0x01 开始测试
	uint16 tail;	
}__attribute__((packed)) RCTestMessage;

typedef struct {
	uint16 head;
	uint8 type;
	uint8 operFlag; 						//0x00 远程关机	   0x01 远程重启
	uint16 tail;	
}__attribute__((packed)) OperDevice;

typedef enum {
	VLAN_TYPE_0 = 0,
	VLAN_TYPE_1,
	VLAN_TYPE_2,
	VLAN_TYPE_3,
	VLAN_TYPE_4,
	VLAN_TYPE_5 
} VlanType;

#define UDP_MULT           0x00     //组播
#define UDP_RADI           0x01     //广播

typedef struct {
	uint8 flag;
	uint8 sourceIp[16];
	uint16 port;
	uint8 type;  	//接收的方式， 0 组播，1 广播
	VlanType vtype;	//唯一区分标记类型
	int offset;	 	//接收数据的偏移量
	//int value;    //对偏移地址的填充数
} UdpParam;



#include "info_apper.h"
typedef struct {
    int fd;					//文件fd
    int flag;				//标记文件存在状态
	uint64 fsize;			//记录文件的大小, 单位B
	FileInfo finfo;			//文件的其它信息
    uint8 fpname[80];    	//文件全名称
} FilePro;

typedef struct {
	int size;						//当前pwf的大小
    uint16 threadId;                //线程id
    pthread_t thread_id;            //线程运行的对象
    uint16 fragment;                //文件分片
    uint8 *directory;               //记录当前时间
    RecvCmd *recv_cmd;              //任务代号
    FilePro *filePro;
    int fileProSize;  				//每个线程可执行最大写入文件的个数
    int fileLimitSize;				//每个文件的的限制大小
	uint64 totalBytes; 				//记录每个线程总共写入了多少B的文件
	uint64 totalVlanBytes;		    //记录每个线程总共写入了多少B的网络字节
	uint64 totalPcieBytes;		    //记录每个线程总共写入了多少B的PCIE字节
	BQueueBox *qBox;             	//记录每个线程写入的总字节数   
    struct OrderQueueBox *oqBox;    //线程队列
} PackWriteFile;

void xprint(uint8 *tdata, uint64 dlen, uint16 rlen);
uint8 getRthFileTypeByChannel(int channel);
#endif